package com.serge.smartdacha;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import static com.serge.smartdacha.SettingsActivity.PREF_PHONE_NUMBER;

public class MainActivity extends Activity {

    public static final String PREF_LATEST_SMS_BODY = "SmartDacha.LatestSMS.body";

    public static final String PREF_LATEST_SMS_TS = "SmartDacha.LatestSMS.timestamp";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab tab1 = actionBar.newTab()
                .setText(R.string.statistics)
                .setIcon(android.R.drawable.ic_menu_view)
                .setTabListener(new SwitchFragmentTabListener(this, MonitorFragment.class));
        actionBar.addTab(tab1);
        ActionBar.Tab tab2 = actionBar.newTab()
                .setText(R.string.control)
                .setIcon(android.R.drawable.ic_menu_edit)
                .setTabListener(new SwitchFragmentTabListener(this, ControlFragment.class));
        actionBar.addTab(tab2);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.settings, menu);
        MenuItem item = menu.findItem(R.id.mi_settings);
        item.setIntent(new Intent(this, SettingsActivity.class));
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void onResume() {
        super.onResume();
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        String phoneNumber = sharedPreferences.getString(SettingsActivity.PREF_PHONE_NUMBER, "");
        getActionBar().setSubtitle(phoneNumber);
    }


    public void sendSmsCommand(String smsCommand) {

        Log.v(MainActivity.class.getSimpleName(), "sendSmsCommand(" + smsCommand + ")");

        SmsManager smsManager = SmsManager.getDefault();
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        smsManager.sendTextMessage(sharedPrefs.getString(PREF_PHONE_NUMBER, ""), null, smsCommand, null, null);

    }

}
